package com.wujilee.wujileejuc.countDownLatch;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author liwuji
 * @date 2024/7/9
 * @desc
 **/
public class CountDownLatchTest {
    private static CountDownLatch startSignal = new CountDownLatch(1);

    private static CountDownLatch endSignal = new CountDownLatch(6);

    public static void main(String[] args) throws InterruptedException {
//        ExecutorService executorService = Executors.newFixedThreadPool(6);
//        for (int i = 0; i < 6; i++) {
//            executorService.execute(() -> {
//                try {
//                    System.out.println(Thread.currentThread().getName() + " 运动员等待裁判员响哨！！！");
//                    startSignal.await();
//                    System.out.println(Thread.currentThread().getName() + "正在全力冲刺");
//                    endSignal.countDown();
//                    System.out.println(Thread.currentThread().getName() + "  到达终点");
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            });
//        }
//        Thread.sleep(100L);
//        System.out.println("裁判员发号施令啦！！！");
//        startSignal.countDown();
//        endSignal.await();
//        System.out.println("所有运动员到达终点，比赛结束！");
        demo();
        while (true){}
    }
    public static void demo() throws InterruptedException {

        CountDownLatch endSignal = new CountDownLatch(6);
        ExecutorService executorService = Executors.newFixedThreadPool(6);
        for (int i = 0; i < 6; i++) {
            executorService.execute(() -> {
                try {
                    Thread.sleep(10000L);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName() + " 运动员等待裁判员响哨！！！");
                    System.out.println(Thread.currentThread().getName() + "正在全力冲刺");
                    endSignal.countDown();
                    System.out.println(Thread.currentThread().getName() + "  到达终点");

            });
        }
        endSignal.await();
        System.out.println("本次任务执行完毕");

    }

}
